package com.borqs.samples.developer;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;

public class HardwareStatus {

    public static abstract class IHardwareSwitch {
        public final String TAG = "IHardwareSwitch ";
        public static final int OPEN = 1;
        public static final int CLOSE = 0;

        public IHardwareSwitch next = null;
        public IHardwareSwitch prev = null;
        public int mCmd = 0;
        public Context context;
        public ArrayList<?> process;

        public IHardwareSwitch(int cmd, ArrayList<?> process) {
            this.mCmd = cmd;
            this.context = App.mContext;
            this.process = process;
        }

        public abstract void setHardSwitch();
    }

    private static final HardwareStatus mInstance = new HardwareStatus();

    IHardwareSwitch mHead;
    IHardwareSwitch mTail;
    int mNumTask = 0;

    public static HardwareStatus getInstance() {
        return mInstance;
    }

    public void addTask(IHardwareSwitch task) {
        if (mHead == null) {
            mHead = mTail = task;
        } else {
            task.prev = mTail;
            mTail.next = task;
            mTail = task;
        }
        mNumTask++;
    }

    public void commit() {
        IHardwareSwitch task = mHead;
        // while (task != null) {
        if (task != null) {
            task.setHardSwitch();
            task = task.next;
        }
        // }
    }

    public int getTaskCount() {
        return mNumTask;
    }

    public static int getWifiState(Context context) {
        boolean wifiState = true;
        WifiManager wifiManger = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        wifiState = wifiManger.isWifiEnabled();
        if (wifiState) {
            return 1;
        } else {
            return 0;
        }
    }

    public static int getSyncState() {
        boolean sync = ContentResolver.getMasterSyncAutomatically();
        if (sync) {
            return 1;
        } else {
            return 0;
        }
    }

    public static int getAirplaneState(Context context) {
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0);
    }

    public static int getGPSState(Context context) {
        boolean gpsState = Settings.Secure.isLocationProviderEnabled(
                context.getContentResolver(), LocationManager.GPS_PROVIDER);
        if (gpsState) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void setAirplaneState(Context context, int state) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, state);
    }

    public static void setSyncState(Context context, int state) {
        if (state == 0) {
            ContentResolver.setMasterSyncAutomatically(false);
        } else {
            ContentResolver.setMasterSyncAutomatically(true);
        }
    }

    public static void setWifiState(Context context, int state) {
        WifiManager wifiManger = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);

        if (state == 0) {
            wifiManger.setWifiEnabled(false);
        } else {
            wifiManger.setWifiEnabled(true);
        }
    }

}
